
package com.braintreepayments.cardform.utils;

import com.braintreepayments.cardform.ResourceTable;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

public class ViewUtils {

    /**
     * 单位转化为像素
     *
     * @param context 上下文
     * @param dp 单位
     * @return px
     */
    public static int dp2px(Context context, float dp) {
        DisplayAttributes attributes = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes();
        return Math.round(attributes.densityPixels * dp);
    }

    /**
     * isDarkBackground
     *
     * @return isDarkBackground
     */
    public static boolean isDarkBackground() {
        int color = ResourceTable.Color_bt_white;
        double luminance = (0.2126 * red(color)) + (0.7152 * green(color)) +
                (0.0722 * blue(color));
        return luminance < 128;
    }

    /**
     * Return the red component of a color int. This is the same as saying
     *
     * @param color color
     * @return (color >> 16) & 0xFF
     */
    private static int red(int color) {
        return (color >> 16) & 0xFF;
    }

    /**
     * Return the green component of a color int. This is the same as saying
     *
     * @param color color
     * @return (color >> 8) & 0xFF
     */
    private static int green(int color) {
        return (color >> 8) & 0xFF;
    }

    /**
     * Return the blue component of a color int. This is the same as saying
     *
     * @param color color
     * @return color & 0xFF
     */
    private static int blue(int color) {
        return color & 0xFF;
    }
}
